package com.hydra.tool.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by ZhengGong on 15/3/23.
 * Description md5加密算法工具
 */
public class MD5Util {
    public static String md5To16(String plainText) {
        return md5To32(plainText).substring(8, 24);
    }

    public static String md5To32(String plainText) {
        String result = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuilder buf = new StringBuilder("");
            for (byte aB : b) {
                i = aB;
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            result = buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(MD5Util.md5To16("20121221"));
        System.out.println(MD5Util.md5To32("20121221"));
    }
}